package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    /**
     * 新增
     *
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult insert(AdSensitive adSensitive) {

        //判断敏感词是否为空
        if (adSensitive == null || adSensitive.getSensitives() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //判断敏感词是否已经存在
        if (StringUtils.isNotBlank(adSensitive.getSensitives())) {
            LambdaQueryWrapper<AdSensitive> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AdSensitive::getSensitives, adSensitive.getSensitives());
            AdSensitive one = getOne(queryWrapper);
            if (one != null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
            }
        }

        adSensitive.setCreatedTime(new Date());
        save(adSensitive);


        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {

        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        AdSensitive adSensitive = getById(id);
        if (adSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        removeById(id);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改
     *
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult update(AdSensitive adSensitive) {

        if (adSensitive == null || adSensitive.getSensitives() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //先查再改
        AdSensitive adSensitive1 = getById(adSensitive.getId());

        if (adSensitive1 == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        updateById(adSensitive);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 敏感词的分页按照关键字模糊查询
     *
     * @param sensitiveDto
     * @return
     */
    @Override
    public ResponseResult findNameByPage(SensitiveDto sensitiveDto) {
        //1、参数校验
        if (sensitiveDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //检查分页
        sensitiveDto.checkParam();
        //2、分页模糊查询
        IPage<AdSensitive> queryParam = new Page<>(sensitiveDto.getPage(), sensitiveDto.getSize());

        LambdaQueryWrapper<AdSensitive> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(sensitiveDto.getName())) {

            query.like(AdSensitive::getSensitives, sensitiveDto.getName());
        }
        IPage<AdSensitive> pageResult = page(queryParam, query);

        PageResponseResult result = new PageResponseResult(sensitiveDto.getPage(), sensitiveDto.getSize(), pageResult.getTotal(), pageResult.getRecords());
        //3、返回结果


        return result;
    }
}
